package demo.Socket.Talking;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/20-08-20-15:40
 * @Description：demo.Socket.Talking
 */
public class talking_Tclient {
    static String name = null;

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        name = sc.nextLine();

        Socket socket = null;

        BufferedReader reader1 = null;
        BufferedReader reader2 = null;
        BufferedWriter writer = null;
        try {
            socket = new Socket("127.0.0.1",6677);

            while(true){
                String s = null;
                reader1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                s = reader1.readLine();
                System.out.println(s);

                reader2 = new BufferedReader(new InputStreamReader(System.in));
                s = reader2.readLine();

                writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                writer.write(name + ": " + s);
                writer.newLine();
                writer.flush();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(reader1!=null){
                    reader1.close();
                }
                if(reader2!=null){
                    reader2.close();
                }
                if(writer!=null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

